package com.bookm.bookm.controller;

import cn.hutool.db.Entity;
import com.bookm.bookm.entity.Book;
import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.BookService;
import com.bookm.bookm.service.CategoryService;
import com.bookm.bookm.service.impl.BookServiceImpl;
import com.bookm.bookm.service.impl.CategoryServiceImpl;
import com.bookm.bookm.utils.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/* 本servlet包含了所有有关图书操作和图书分类的操作 */
@WebServlet("/book")
public class BookServlet extends HttpServlet {
    /* 图书操作接口 */
    private BookService service = new BookServiceImpl();
    /* 图书分类接口 */
    private CategoryService categoryService = new CategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String key = req.getParameter("key");
        switch (key) {
            // 获取图书列表
            case "getBookList":
                getBookList(req, out);
                break;
            // 获取所有分类
            case "allCategory":
                allCategory(out);
                break;
            // 图书添加
            case "addBook":
                addBook(req, out);
                break;
            // 图书列表
            case "bookListAll":
                out.print(service.bookListAll());
                break;
            // 图书信息更新
            case "updateBook":
                updateBook(req, out);
                break;
            // 图书删除
            case "delBook":
                delBook(req, out);
            case "getBook":
                getBook(req, out);
        }
    }
    // 获取图书信息
    private void getBook(HttpServletRequest req, PrintWriter out){
        String bookId = req.getParameter("bookId");
        Book book = service.getBook(bookId);
        out.print(ResultUtil.success(book));
    }
    // 图书删除
    private void delBook(HttpServletRequest req, PrintWriter out) {
        // 获取图书编号
        String bookId = req.getParameter("bookId");
        out.print(service.delBook(bookId));
    }
    // 图书添加
    private void addBook(HttpServletRequest req, PrintWriter out) {
        // 图书编号
        String id = req.getParameter("bookId");
        // 图书名称
        String name = req.getParameter("bookName");
        // 作者
        String author = req.getParameter("author");
        // 出版社
        String pub = req.getParameter("pub");
        // 图书价格
        Double price = Double.parseDouble(req.getParameter("price"));
        // 图书描述
        String descr = req.getParameter("descr");
        // 图书分类
        Integer class_ = Integer.parseInt(req.getParameter("class_"));
        // 图书数量
        Integer num = Integer.parseInt(req.getParameter("num"));
        // 图书封面图
        String imgUrl = req.getParameter("imgUrl");
        Book book = new Book(id, name, author, pub, price, descr, class_, num, imgUrl);
        // 设置图书添加日期
        book.setRdDate(new Date());
        Result data = service.addBook(book);
        out.print(data);
    }
    // 获取图书列表
    private void getBookList(HttpServletRequest req, PrintWriter out) {
        // 当前页
        Integer curPage = Integer.parseInt(req.getParameter("curPage"));
        // 每页大小
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        // 图书类别id
        Integer cid = Integer.parseInt(req.getParameter("cid"));
        // 搜索文本
        String searText = req.getParameter("searText");
        Result bookList = service.getBookList(curPage, pageSize, Entity.create("book"), cid, searText);
        out.print(bookList.toString());
    }
    // 获取图书分类
    public void allCategory(PrintWriter out) {
        Result data = categoryService.getAll();
        out.print(data.toString());
    }

    // 图书信息更新
    private void updateBook(HttpServletRequest req, PrintWriter out) {
        String id = req.getParameter("bookId");
        String name = req.getParameter("bookName");
        String author = req.getParameter("author");
        String pub = req.getParameter("pub");
        Double price = Double.parseDouble(req.getParameter("price"));
        String descr = req.getParameter("descr");
        Integer class_ = Integer.parseInt(req.getParameter("class_"));
        Integer num = Integer.parseInt(req.getParameter("num"));
        String imgUrl = req.getParameter("imgUrl");
        Book book = new Book(id, name, author, pub, price, descr, class_, num, imgUrl);
        out.print(service.updateBook(book));
    }


}
